<?php
defined('SYSPATH') or die('Access error!');
/**
 * 用户的站内信类
 * ============================================================================
 * 功能描述： 主要是处理信件的发送和接受,以及系统的信件.
 * ============================================================================
 * $Author: liuxudong $
 */

class Controller_User_Message extends Controller_User_Main
{

    private $message;
    private $user_type = 'from_user';
    private $where_arr = array();

    public function before()
    {
        parent::before();
        $this->message = Model::factory('user_message');
        $this->set_define_position(__('user message'));
        $this->where_arr['parent_id'] = 0;
        $this->template->assign('current_selected', Url::site('user/message/receive_new'));
    }

    
    public function action_send_list()
    {
        Breadcrumbs::add(__('sended message'));
        $this->where_arr['from_user'] = $this->user_detail['id'];
        $this->user_type = 'to_user';
        $this->message_list();
    }

    public function action_receive_new()
    {
        Breadcrumbs::add(__('new message'));
        $this->where_arr['to_user'] = $this->user_detail['id'];
        $this->where_arr['is_read'] = 0;
        $this->message_list();
    }

    public function action_receive_readed()
    {
        Breadcrumbs::add(__('readed message'));
        $this->where_arr['to_user'] = $this->user_detail['id'];
        $this->where_arr['is_read'] = 1;
        $this->message_list();
    }

    public function action_system()
    {
        Breadcrumbs::add(__('system message'));
        $this->where_arr['to_user'] = $this->user_detail['id'];
        $this->where_arr['from_user'] = 0;
        $this->message_list();
    }

    public function message_list()
    {
        $message_list = $this->message->get_message_list($this->where_arr, $this->user_type);
        $this->template->assign('message_list', $message_list['list']);
        $this->template->assign('page', $message_list['page']);
        $this->template->view('user_message_list');
    }

    public function action_view()
    {
        Breadcrumbs::add(__('view message'));
        $id = $this->request->param('id');
        if (empty($id))
        {
            Message::error('no exist');
            $this->request->redirect('user/message/receive_new');
        }
        $users = $this->message->get_row(array('id' => $id), 'from_user,to_user');
        if (Arr::validate_arr($users))
        {
            if (!in_array($this->user_detail['id'], $users))
            {
                Message::error('no exist');
                $this->request->redirect('user/message/receive_new');
            }
        }
        else
        {
            Message::error(__('no exist'));
            $this->request->redirect('user/message/receive_new');
        }

        $this->message->update(array('is_read' => 1), array('id' => $id));
        $message_detail = $this->message->get_message_detail($id);
        $message_children = $this->message->get_message_all_children($id);
        $parent_arr = array();

        if (count($message_children) == 1)
        {
            $parent_arr['parent_id'] = $message_detail['id'];
            $parent_arr['from_user'] = $message_detail['from_user'];
        }
        elseif (Arr::validate_arr($message_children) && count($message_children) > 1)
        {
            $message_children_temp = array_reverse($message_children);
            foreach ($message_children_temp as $value)
            {
                if ($value['from_user'] != $this->user_detail['id'])
                {
                    $parent_arr['parent_id'] = $value['id'];
                    $parent_arr['from_user'] = $value['from_user'];
                    break;
                }
            }
        }
        $this->template->assign('data', $message_detail);
        $this->template->assign('parent_arr', $parent_arr);
        $this->template->assign('message_children', $message_children);
        $this->template->view('user_message_view');
    }

    //信件的回复
    public function action_reply()
    {
        if ($_POST)
        {
            $id = Arr::get($_POST, 'id');
            $_POST['from_user'] = $this->user_detail['id'];
            $_POST['is_read'] = 1;
            if ($this->message->send_message($_POST))
            {
                Message::success('发送成功');
                $this->request->redirect('user/message/view/' . $id);
            }
        }
    }

    public function action_send()
    {
        Breadcrumbs::add(__('send message'));
        if ($_POST)
        {
            if (!$this->message->validate($_POST))
            {
                Message::error(__('validate error'));
                $this->request->redirect('user/message/send');
            }
            $to_username = Arr::get($_POST, 'to_user');
            $to_user = Model::factory('user')->get_var(array('username' => $to_username), 'id');
            if (empty($to_user))
            {
                Message::error('用户不存在');
                $this->request->redirect('user/message/send');
            }
            $_POST['to_user'] = $to_user;
            $_POST['from_user'] = $this->user_detail['id'];
            if ($this->message->send_message($_POST))
            {
                Message::success('发送成功');
                $this->request->redirect('user/message/send');
            }
        }
        $this->template->view('user_message_send');
    }

}